package com.hackcloud.shardingjdbc.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.hackcloud.shardingjdbc.dao.center.ApplyCenterDao;
import com.hackcloud.shardingjdbc.dao.center.ApplyCenterMainRepository;
import com.hackcloud.shardingjdbc.dao.center.ApplyCenterValidatorRepository;
import com.hackcloud.shardingjdbc.domain.center.ApplyCenterMain;
import com.hackcloud.shardingjdbc.domain.center.Validator;
import com.hackcloud.shardingjdbc.excel.template.ValidatorLog;
import com.hackcloud.shardingjdbc.excel.template.ValidatorReadListener;
import com.hackcloud.shardingjdbc.service.ApplyCenterServiceFactory;
import com.hackcloud.shardingjdbc.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.criteria.Predicate;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class TestController {

    @Autowired
    ApplyCenterServiceFactory applyCenterServiceFactory;
    @Autowired
    ApplyCenterMainRepository applyCenterMainRepository;

    @Autowired
    SysConfigService sysConfigService;

    @Autowired
    ApplyCenterDao applyCenterDao;
    @Autowired
    @Qualifier("shardingManagerFactoryBean")
    protected EntityManager em;

    @PostMapping("/apply")
    public String saveAll(@RequestBody String json) {
        applyCenterServiceFactory.execute(json);
        return "SUCCESS";
    }

    protected static Sort sort = new Sort(Sort.Direction.ASC, "id");

    Long maxId = null;

    @GetMapping("/query")
    public void findAll() {
        int index = 1;
        if (maxId != null) {
            index = 0;
        }
        PageRequest pageRequest = new PageRequest(0, 1, sort);
        Slice<ApplyCenterMain> applyCenterDaoAll = applyCenterDao.findAll(getSpecialQuery(maxId), pageRequest, ApplyCenterMain.class);
        applyCenterDaoAll.forEach(x -> {
            maxId = x.getId();
        });
        System.out.println(JSON.toJSONString(applyCenterDaoAll));

    }

    private Specification<ApplyCenterMain> getSpecialQuery(Long maxId) {
        Specification<ApplyCenterMain> query = (root, criteriaQuery, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (maxId != null) {
                predicates.add(cb.greaterThan(root.get("id").as(Long.class), maxId));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return query;
    }

    @GetMapping("/read")
    @ResponseBody
    public List<ApplyCenterMain> read() {
        List<ApplyCenterMain> applyCenterMains = applyCenterServiceFactory.read();
        return applyCenterMains;
    }

    @GetMapping("/search")
    @ResponseBody
    public String search(String method, String param) throws Exception {
        Method method1 = ApplyCenterMainRepository.class.getDeclaredMethod(method, String.class);
        Object invoke = method1.invoke(applyCenterMainRepository, param);
        return JSON.toJSONString(invoke);
    }

    @PostMapping("/sys")
    public String sys() {
        sysConfigService.save();
        return "SUCCESS";
    }

    @Autowired
    ApplyCenterValidatorRepository applyCenterValidatorRepository;

    @GetMapping("/validator")
    @ResponseBody
    public String validator(String productNo) {
        String fileName = "C:\\Users\\FM\\Desktop\\" + productNo + ".xls";
        // 这里默认读取第一个sheet
        ValidatorReadListener readListener = new ValidatorReadListener();
        EasyExcel.read(fileName, ValidatorLog.class, readListener).sheet().doRead();
        log.warn("PRODUCT:{}  SIZE:{}", productNo, readListener.getValidatorList().size());
        List<Validator> validatorList = readListener.getValidatorList();
        applyCenterValidatorRepository.save(validatorList);
        return "SUCCESS";
    }
}
